const jwt = require('jsonwebtoken');
const config = require('../db/config.js')
//必须要有token（需要用户登录）

module.exports = async ( ctx,next ) => {
    const authorization = ctx.request.header['cookie']
    if( authorization =='') {
        ctx.body = {
            code:500,
            massage:'用户未登录'
        }
        return false
    }

    const token = authorization;
    let tokenContent;
    try {
        tokenContent = await jwt.verify(token.split('=')[1],config.secret) // //如果token过期或验证失败，将抛出错误

        //ctx.userInfo = tokenContent
        return tokenContent
    } catch (err) {
        ctx.body = {
            code:500,
            massage:'用户登录验证失败'
        }
    }

}